package com.starblink.login.forgotpassword.inputemail.ui

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.apollographql.apollo3.api.Optional
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.rocketreserver.CheckingAccountDoesItExistQuery
import kotlinx.coroutines.launch

class ForgotPswInputEmailVM : BaseViewModel() {
    val checkResultLiveData: MutableLiveData<Triple<Boolean, String?, String?>> = MutableLiveData()

    /**
     * 根据邮箱或用户名，检查账户是否存在
     */
    fun checkAccountExist(email: String?, userName: String?) {
        viewModelScope.launch {
            val response = SkGqlClient.skQuery(
                CheckingAccountDoesItExistQuery(
                    email = Optional.presentIfNotNull(email),
                    username = Optional.presentIfNotNull(userName)
                ),
                showDefaultLoading = true
            )
            if (response?.data != null && response.data!!.checkingAccountDoesItExist != null) {
                checkResultLiveData.postValue(
                    Triple(
                        response.data!!.checkingAccountDoesItExist!!,
                        email,
                        userName
                    )
                )
            }
        }
    }
}